package cn.swing.his.element.webapi.controller;

import cn.swing.his.common.web.JsonResult;
import cn.swing.his.element.service.IElementService;
import cn.swing.his.pojo.element.dto.CarouselDTO;
import cn.swing.his.pojo.element.dto.MenuBarDTO;
import cn.swing.his.pojo.element.vo.CarouselVO;
import cn.swing.his.pojo.element.vo.MenuBarVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/elements",produces = "application/json;charset=utf-8")
@Api(tags = "轮播图导航栏管理")
public class ElementController {
    @Autowired(required = false)
    private IElementService iElementService;


    @PostMapping("/upload")
    public JsonResult<String> uploadImage(@RequestParam("file")MultipartFile file){
        String imageAdress = iElementService.uploadImage(file);
        if (imageAdress == null) {
            return JsonResult.failed("上传失败");
        }
        else {
            return JsonResult.ok("上传成功",imageAdress);
        }
    }

    @GetMapping("/listCarousel")
    @ApiOperation("轮播图")
    public JsonResult<List<CarouselVO>> list() {
        List<CarouselVO> list = iElementService.list();
        return JsonResult.ok("轮播图",list);
    }
    @GetMapping("/updateCarousel")
    @ApiOperation("修改轮播图")
    public JsonResult<Integer> updateCarousel( CarouselVO carousel){
        Integer result = iElementService.updateCarousel(carousel);
        return JsonResult.ok("update",result);
    }

    @GetMapping("/deleteCarousel")
    @ApiOperation("删除轮播图")
    public JsonResult<Integer> deleteCarousel( Integer id) {
        Integer result =iElementService.deleteCarousel(id);
        return JsonResult.ok("delete",result);
    }

    @PostMapping("/insertCarousel")
    @ApiOperation("插入轮播图")
    public JsonResult<Integer> insertCarousel(@RequestBody CarouselDTO carousel) {
        Integer result = iElementService.insertCarousel(carousel);
        return JsonResult.ok("insert",result);
    }

    @GetMapping("/listMenuBar")
    @ApiOperation("导航栏")
//    导航栏管理
    public JsonResult<List<MenuBarVO>> listMenuBar() {
        List<MenuBarVO> list = iElementService.listMenuBar();
        return JsonResult.ok("导航栏",list);
    }

    @GetMapping("/updateMenuBar")
    @ApiOperation("修改导航栏")
    public JsonResult<Integer> updateMenuBar(MenuBarDTO menuBar){
        Integer result = iElementService.updateMenuBar(menuBar);
        return JsonResult.ok("update",result);
    }

    @GetMapping("/deleteMenuBar")
    @ApiOperation("删除导航栏")
    public JsonResult<Integer> deleteMenuBar( Integer id) {
        Integer result = iElementService.deleteMenuBar(id);
        return JsonResult.ok("delete",result);
    }

    @PostMapping("/insertMenuBar")
    @ApiOperation("插入导航栏")
    public JsonResult<Integer> insertMenuBar(@RequestBody MenuBarDTO menuBar) {
        Integer result = iElementService.insertMenuBar(menuBar);
        return JsonResult.ok("insert",result);
    }
//    //上传图片
//    @PostMapping("/uploadArticlePicture")
//    @ApiOperation("上传图片")
//    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
//        String fileName = iElementService.uploadPictures(picFile);
//        return JsonResult.ok("ok",fileName);
//    }
    //删除上传的图片
    @GetMapping("/removePicture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        iElementService.removePicture(pictureName);
        return JsonResult.ok();
    }
}

